package com.hx.servlet;

import com.hx.dao.impl.BizDaoImpl;
import com.hx.entity.Admin;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "UpdateServlet",urlPatterns = {"/UpdateServlet"})
public class UpdateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        HttpSession session = request.getSession();
        Admin admin = new Admin();
        BizDaoImpl biz = new BizDaoImpl();
        Integer aid = (Integer) session.getAttribute("aid");
        admin = biz.get_AidInfo2(aid);
        System.out.println("----->"+aid);
        String password = request.getParameter("password");
        String password2 = request.getParameter("password2");
        String old_password  = admin.getPassword();
        if (old_password.equals(password)){
            biz.updateUser(admin.getAid(),password2);
            response.sendRedirect(request.getContextPath()+"/index.jsp");
        }else{
            out.write("<script type='text/javascript'>" +
                    "alert('password error')" +
                    "</script>");
        }
    }
}
